<?php

/**
 * buses actions.
 *
 * @package    SistemaRutasEc
 * @subpackage buses
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class busesActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {

        $idco = $request->getParameter('id_co');
        $this->getUser()->setAttribute('id_co', $idco);
        $q = Doctrine_Query::create()
                ->from('cooperativa c')
                ->where('c.id_co=?', $idco);
        $this->coop = $q->fetchOne();
        $this->buss = Doctrine::getTable('bus')->createQuery('b')
                ->where('b.id_co = ?', $idco)
                ->execute();
       
    }

    public function executeShow(sfWebRequest $request) {
        $this->bus = Doctrine_Core::getTable('bus')->find(array($request->getParameter('id_bu')));
        $this->forward404Unless($this->bus);
    }

    public function executeNew(sfWebRequest $request) {
        $this->idco = $request->getParameter('id_co');
        $this->getUser()->setAttribute('id_co', $this->idco);
        $this->form = new busForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new busForm();

        $this->processFormNew($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($bus = Doctrine_Core::getTable('bus')->find(array($request->getParameter('id_bu'))), sprintf('Object bus does not exist (%s).', $request->getParameter('id_bu')));
        $this->idco=$bus->getIdCo();
        $this->form = new busForm($bus);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($bus = Doctrine_Core::getTable('bus')->find(array($request->getParameter('id_bu'))), sprintf('Object bus does not exist (%s).', $request->getParameter('id_bu')));
        $this->form = new busForm($bus);

        $this->processFormEdit($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($bus = Doctrine_Core::getTable('bus')->find(array($request->getParameter('id_bu'))), sprintf('Object bus does not exist (%s).', $request->getParameter('id_bu')));
        $aux=$bus->getIdCo();
        $bus->delete();
        $this->getUser()->setFlash('notice', 'Unidad eliminada exitosamente');
        $this->redirect('buses/index?id_co=' . $aux);        
    }

    protected function processFormNew(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            
            $form->getObject()->setIdCo($this->getUser()->getAttribute('id_co'));            
            $bus = $form->save();            
            $this->getUser()->setFlash('notice', 'Unidad creada exitosamente');
            $this->redirect('buses/index?id_co=' . $bus->getIdCo());
        }
    }
    
    protected function processFormEdit(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $bus = $form->save();
            $this->getUser()->setFlash('notice', 'Datos de la Unidad editados exitosamente');
            $this->redirect('buses/index?id_co=' . $bus->getIdCo());
            
        }
    }

}
